package com.hlm.transparent.core.resolve;

import com.hlm.transparent.model.TransparentData;
import com.hlm.transparent.util.ComUtil;
import com.hlm.transparent.util.HexUtil;
import com.hlm.transparent.util.ProtocolHelper;
import io.netty.buffer.ByteBuf;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.function.DoubleBinaryOperator;

/**
 * @program:transparent
 * @description:解析CJT188协议
 * @author: xinY
 **/
public class CJT188Resolve {
    public static TransparentData resolve(ByteBuf byteBuf, String protocolCode){
        Map<String,Object> resultMap = new HashMap<>();
        Map<String,Object> sectionMap = ProtocolHelper.getParameterMap(protocolCode,"command");
        if (sectionMap == null) {
            System.out.println("parse.ini不存在该协议");
            return null;
        }
        int start = ComUtil.skipFE(byteBuf);
        // 解析地址
        Integer addrOrder = (int)sectionMap.get("addrorder");
        String addr = "";
        if ( 1 == addrOrder) {// 逆序
            for (int i = start + 8 ; i >= start +2 ; i -- ){
                 addr = addr + HexUtil.getBCD(byteBuf.getByte(i));
            }
        }else{
            for (int i = start + 2 ; i <= start + 8 ; i ++){
                addr = addr + HexUtil.getBCD(byteBuf.getByte(i));
            }
        }
        resultMap.put("addr",addr);
        // 解析变量
        // 有几条命令
        Object variableNum1 = sectionMap.get("variablenum"+(0+1));
        for (int i = 0; i < ComUtil.safeInt(variableNum1); i++) {
            // parse.ini 的section 后缀
            String section = (0+1) + "_var" + i;
            // 变量属性
            Map<String,Object> map = ProtocolHelper.getParameterMap(protocolCode,section);
            assert map != null;
            int dataStart = ComUtil.safeInt( map.get("datastart"));
            int dataEnd = ComUtil.safeInt(map.get("dataend"));
            int dataOrder = ComUtil.safeInt(map.get("dataorder"));
            int dataType = ComUtil.safeInt(map.get("datatype"));
            int dataPoint = ComUtil.safeInt(map.get("datapoint"));
            int dataUnit = ComUtil.safeInt(map.get("dataunit"));
            String varType = ComUtil.safeString(map.get("vartype"));
            String unit = (String)map.get("unit");
            System.out.println("var:"+i+"\tstart:"+dataStart+"\tend:"+dataEnd+"\torder:"+dataOrder+"\ttype:"+dataType+"\tpoint:"+dataPoint+"\tunit:"+dataUnit+"\tunit"+unit+"\t");
            // 顺序  ： 直接拼接字符串
            if (dataOrder == 2 ){
               String var = "";
                // BCD
                if ( 0 == dataType){
                    for (int j = dataStart ; j <= dataEnd ; j++){
                        var = var + HexUtil.getBCD(byteBuf.getByte(j));
                        resultMap.put(varType , var);
                    }
                }
                // hex
                else{
                    for (int j = dataStart ; j <= dataEnd ; j++){
                        var = var + byteBuf.getByte(j) ;
                        resultMap.put(varType, var);
                    }
                }
            }
            // 倒叙
            else{
                Double var = 0.0;
                // BCD
                if ( 0 == dataType){
                    // 时间
                    if("0F".equals(varType)){

                        String date = "";
                        for (int j = dataEnd ; j >= dataStart ; j --){
                            // 补零 拼接
                            date = date + ComUtil.fillZeroLeft(HexUtil.getBCD(byteBuf.getByte(j)),2) ;
                            resultMap.put(varType,date);
                        }
                    }
                    else{

                        Double point = Math.pow(0.1, dataPoint);
                        for (int j = dataStart ; j <=dataEnd ; j ++){
                            var = var + HexUtil.getBCD(byteBuf.getByte(j)) * point ;
                            point = point * 100 ;
                            resultMap.put(varType, var);
                        }
                    }
                }
                //
                else{
                    Double point = Math.pow(0.1, dataPoint);
                    for (int j = dataEnd ; j >= dataStart ; j --){
                        var = var + byteBuf.getByte(j) * point ;
                        point = point * 100 ;
                        resultMap.put(varType , var);
                    }
                }

            }
        }
        // 解析state
        Map<String,Object> stateMap = ProtocolHelper.getParameterMap(protocolCode,"state");
        // 第一个字节默认没有故障
        resultMap.put("faultType1","无故障");
        if ( 0 != byteBuf.getByte(byteBuf.readableBytes()-4)){
            resultMap.replace("faultType1",stateMap.get("name1"));
        }
        // 第二个字节默认没有故障
        resultMap.put("faultType2","无故障");
        for (int i = 2 ; i <= 5 ; i++ ){
            // TODO: 2020/8/20 这个地方逻辑有些问题， 如果有很多个故障我现在只能取一个
            if ( 0 != (byteBuf.getByte(byteBuf.readableBytes()-3) >> (5-i))){
                resultMap.replace("faultType2",stateMap.get("name"+i));
                break;
            }
        }
        for(String key : resultMap.keySet()){
            System.out.println(key+"：：：：：：：：：：：：：："+resultMap.get(key));
        }
        // mongo 实体类
        TransparentData transparentData = new TransparentData();
        transparentData.setDataMap(resultMap);
        return transparentData;
    }
}
